package com.hzp.micromall.content.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzp.micromall.common.core.api.CommonPage;
import com.hzp.micromall.content.dao.SubjectDao;
import com.hzp.micromall.content.entity.Subject;
import com.hzp.micromall.content.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 专题表 服务实现类
 * </p>
 *
 * @author houzipeng
 * @since 2022-03-08 10:15:14
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectDao, Subject> implements SubjectService {

    @Autowired
    private SubjectDao subjectDao;

    @Override
    public List<Subject> listAll() {
        return subjectDao.selectList(null);
    }

    @Override
    public CommonPage<Subject> getPage(String keyword, Integer pageNum, Integer pageSize) {
        Page<Subject> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Subject> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(keyword)){
            wrapper.like(Subject::getTitle, keyword);
        }
        subjectDao.selectPage(page, wrapper);
        return new CommonPage<>(page.getCurrent(), page.getTotal(), page.getSize(), page.getRecords());
    }

    @Override
    public int update(Long id, Subject subject) {
        subject.setId(id);
        return subjectDao.updateById(subject);
    }

    @Override
    public int updateShowStatus(List<Long> ids, Integer newShowStatus) {
        Subject subject = new Subject();
        subject.setShowStatus(newShowStatus);
        LambdaQueryWrapper<Subject> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Subject::getId, ids);
        return subjectDao.update(subject, wrapper);
    }

    @Override
    public int delete(List<Long> ids) {
        return subjectDao.deleteBatchIds(ids);
    }
}
